source('00_util_scripts/mod_seurat.R')
source('00_util_scripts/mod_bplot.R')

sobj <- read_rds('ma2024_tib/panB_scRNA_processed_data.rds')

sobj |> dplyr::count(dataid_type)

sobj |> VlnPlot('percent.mt', pt.size = 0, group.by = 'site')

fcgr2b.patient <- sobj |> get_abundance_sc_long('FCGR2B') |>
  left_join(x = sobj, y = _) |>
  summarise(mean.fcgr2b = ExpMean(.abundance_RNA), .by = patient)

fcgr2b.patient <- fcgr2b.patient |>
  mutate(rank.2b = percent_rank(mean.fcgr2b),
         expr2b.q2 = ifelse(rank.2b > .5, 'high', 'low'),
         expr2b.q3 = case_when(rank.2b > 2/3 ~ 'high',
                               rank.2b < 1/3 ~ 'low',
                               .default = 'mid'))

fcgr2b.patient |>
  ggplot(aes(mean.fcgr2b, expr2b.q3)) +
  geom_point()

fcgr2b.patient |>
  write_csv('ma2024_tib/patient.2b.rank.csv')
